package com.xhwl.centiotdata.suppliersystem.guangtuo;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xhwl.centiotdata.pojo.constant.GuangTuoAlarmHostConstant;
import com.xhwl.centiotdata.pojo.dto.alarmhost.DeviceGuangTuoAlarmHostDTO;
import com.xhwl.centiotdata.pojo.dto.alarmhost.DeviceStatusGuangTuoAlarmHostDTO;
import com.xhwl.centiotdata.pojo.dto.alarmhost.DeviceSubSystemStatusGuangTuoAlarmHostDTO;
import com.xhwl.centiotdata.pojo.dto.device.DeviceIntegrationConfigDTO;
import com.xhwl.common.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author zhouyongzhou
 * @date 2023/04/20  16:34
 */
@Slf4j
@Service
public class DeviceGuangTuoAlarmHostHandler {

    @Resource
    private GeneralGuangTuoAlarmHostHandler generalGuangTuoAlarmHostHandler;

    public List<DeviceGuangTuoAlarmHostDTO> pullDeviceList(DeviceIntegrationConfigDTO configDTO) {
        String deviceListStr = getListStr(configDTO.getIpAndPort(), GuangTuoAlarmHostConstant.PULL_ALL_RECORD_URL);
        List<DeviceGuangTuoAlarmHostDTO> deviceGuangTuoAlarmHostDTOList = JSONObject.parseArray(deviceListStr,DeviceGuangTuoAlarmHostDTO.class);
        return deviceGuangTuoAlarmHostDTOList;
    }

    public List<DeviceStatusGuangTuoAlarmHostDTO> pullDeviceStatusList(DeviceIntegrationConfigDTO configDTO) {
        String deviceStatusListStr = getListStr(configDTO.getIpAndPort(), GuangTuoAlarmHostConstant.PULL_DEVICE_STATUS_URL);
        List<DeviceStatusGuangTuoAlarmHostDTO> deviceStatusGuangTuoAlarmHostDTOList = JSONObject.parseArray(deviceStatusListStr, DeviceStatusGuangTuoAlarmHostDTO.class);
        return deviceStatusGuangTuoAlarmHostDTOList;
    }

    public List<DeviceStatusGuangTuoAlarmHostDTO> pullDeviceStatusByObjId(String ipAndPort,String objId) {
        String deviceStatusListStr = getListStr(ipAndPort,new StringBuilder(GuangTuoAlarmHostConstant.PULL_DEVICE_STATUS_URL).append("?objID=").append(objId).toString());
        List<DeviceStatusGuangTuoAlarmHostDTO> deviceStatusGuangTuoAlarmHostDTOList = JSONObject.parseArray(deviceStatusListStr, DeviceStatusGuangTuoAlarmHostDTO.class);
        return deviceStatusGuangTuoAlarmHostDTOList;
    }

    public List<DeviceSubSystemStatusGuangTuoAlarmHostDTO> pullDeviceSubSystemStatusList(DeviceIntegrationConfigDTO configDTO) {
        String DeviceSubSystemStatusStr = getListStr(configDTO.getIpAndPort(), GuangTuoAlarmHostConstant.PULL_SUB_SYSTEM_STATUS_URL);
        List<DeviceSubSystemStatusGuangTuoAlarmHostDTO> deviceSubSystemStatusGuangTuoAlarmHostDTOList = JSONObject.parseArray(DeviceSubSystemStatusStr, DeviceSubSystemStatusGuangTuoAlarmHostDTO.class);
        return deviceSubSystemStatusGuangTuoAlarmHostDTOList;
    }

    /**
     * 防区布防/撤防/旁路
     * @param objID 防区id
     * @param defAreaCtrlType 控制类型
     */
    public boolean putDefAreaCtrlType(String ipAndPort, String objID, String defAreaCtrlType) {
        String url = new StringBuilder().append(ipAndPort).append(GuangTuoAlarmHostConstant.PUT_DEF_AREA_CTRL_TYPE_URL)
                .append("?objID=").append(objID).append("&defAreaCtrlType=").append(defAreaCtrlType).toString();
        return putCtrlType(url);
    }

    /**
     *子系统布防/撤防/旁路控制
     * @param subSystemNo 子设备编号
     * @param subSystemCtrlType 控制类型
     */
    public boolean putSubSystemCtrlType(String ipAndPort,String subSystemNo,String subSystemCtrlType){
        String url = new StringBuilder().append(ipAndPort).append(GuangTuoAlarmHostConstant.PUT_SUB_SYSTEM_CTRL_TYPE_URL)
                .append("?subSystemNo=").append(subSystemNo).append("&subSystemCtrlType=").append(subSystemCtrlType).toString();
        return putCtrlType(url);
    }


    public String getListStr(String ipAndPort,String apiUrl){
        String url = new StringBuilder(ipAndPort).append(apiUrl).toString();
        JSONObject resultObject = generalGuangTuoAlarmHostHandler.get(url);
        log.info("拉取设备结果:" + resultObject);
        if (resultObject == null) {
            return null;
        }
        if ("false".equals(resultObject.getString("success"))) {
            log.error("get path:{};params:{};msg:{}", apiUrl, null, resultObject.getString("msg"));
            return null;
        }
        String dataStr = resultObject.getString("data");
        String listStr = JSON.parseObject(dataStr).getString("list");
        if (StringUtils.isEmpty(listStr)) {
            log.error("get path:{};params:{};data is null!", apiUrl, null);
            return null;
        }
        return listStr;
    }


    public boolean putCtrlType(String path) {
        JSONObject resultObject = generalGuangTuoAlarmHostHandler.put(path);
        log.info("修改设备状态:" + resultObject);
        if (resultObject == null) {
            return false;
        }
        if ("false".equals(resultObject.getString("success"))) {
            log.error("put path:{};;msg:{}", path, resultObject.getString("msg"));
            return false;
        }
        return "true".equals(resultObject.getString("success"));
    }

}
